<template>
  <div>
    <div class="Searchbox">
      <el-card class="box-card" shadow="never">
        <el-form ref="form" :model="form" label-width="120px">
          <el-row :gutter="20">
            <el-col :span="5">
              <el-form-item label="核心工作名称">
                <el-input v-model="seleceDeptToPage.name" style="width:200px;"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item label="职能部门" label-width="100px">
                <el-select v-model="seleceDeptToPage.deptId" style="width:200px;"  placeholder="全部">
                  <el-option label="全部"></el-option>
                  <el-option :label="item.deptName" :value="item.id" :key="item.id" v-for="item in DeptToPage"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button type="primary" @click="getCompanyToPages(1)" icon="el-icon-search">搜索</el-button>
            </el-col>

            <el-col :span="4">
              <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true,flag=1,disabled= false,AddDeptData={},title='添加核心工作'">添加</el-button>
            </el-col>
          </el-row>
          <!-- 
          <el-row :gutter="4">
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true,flag=1,disabled= false,AddDeptData={},title='添加职能部门'">添加</el-button>
          </el-row>-->
        </el-form>
      </el-card>
      <br />
      <el-card class="box-card" shadow="never">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="coreName" label="核心工作名称" width="250"></el-table-column>
          <el-table-column prop="deptName" label="职能部门名称" width="250"></el-table-column>
          <el-table-column prop="sort" label="排序" width="80"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="200">
            <template slot-scope="scope">{{scope.row.createTime | time}}</template>
          </el-table-column>
          <el-table-column prop="remarks" label="备注"></el-table-column>
          <el-table-column fixed="right" label="操作" width="250">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row.id),disabled = true" type="text" size="small">查看</el-button>
              <el-button type="text" size="small" @click="eidtData(scope.row),title='编辑核心工作'">编辑</el-button>
              <el-button @click="doDeleteDept(scope.row.id)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagebox">
          <el-pagination background layout="prev, pager, next" :page-size="seleceDeptToPage.pageSize" :total="dataCount" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </el-card>
    </div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-position="right" :disabled="disabled">
        <el-form-item label="核心工作名称" label-width="100px">
          <el-input v-model="AddDeptData.coreName" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="职能部门" label-width="100px">
          <el-select v-model="AddDeptData.deptId" style="width:200px;"  placeholder="职能部门">
            <el-option :label="item.deptName" :value="item.id" :key="item.id" v-for="item in DeptToPage"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="排序" label-width="100px">
          <el-input v-model="AddDeptData.sort" type="number" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="备注" label-width="100px">
          <el-input type="textarea" v-model="AddDeptData.remarks" style="width:200px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false,AddDeptData = {}" v-if="!disabled">取 消</el-button>

        <el-button type="primary" v-if="flag==2" @click="dialogFormVisible = false">确 定</el-button>
        <el-button type="primary" v-if="flag==1" @click="getaddCompany()">确 定</el-button>
        <el-button type="primary" v-if="flag==3" @click="doupdateDeptMessage()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  AddWork,
  getWorkToPage,
  updateWorkMessage,
  DeleteWork,
  getSysWorkById,
  getAllDeptToList,
} from "@/api/api";

export default {
  data() {
    return {
      title: "",
      flag: 0,
      dialogFormVisible: false,
      disabled: true,
      seleceDeptToPage: {
        name: "",
        deptId:"",
        pageIndex: 1,
        pageSize: 10,
      },
      form: {},
      AddDeptData: {},
      tableData: [],
      dataCount: 0,
      DeptToPage: [],
    };
  },
  mounted() {
    let that = this;
    that.getCompanyToPages(1);
    that.getAllDeptToLists();
  },

  filters: {
    time(value) {
       
       let values = value.split(".")[0]
       return values.replace(/T/g, " ");
    },
  },

  methods: {
    goBack() {
    },

    async doDeleteDept(id) {
      let that = this;
      let params = {
        id: id,
      };
      let res = await DeleteWork(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.getCompanyToPages(that.seleceDeptToPage.pageIndex);
      }else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async getAllDeptToLists() {
      let that = this;
      let params = {};
      let res = await getAllDeptToList(params);
      if (res.data.Code == 200) {
        that.DeptToPage = res.data.Data;
      }else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    eidtData(row) {
      let that = this;
      that.dialogFormVisible = true;
      that.AddDeptData = row;
      that.flag = 3;
      that.disabled = false;
    },

    async doupdateDeptMessage() {
      let that = this;

      if (
        that.AddDeptData.coreName == undefined ||
        that.AddDeptData.coreName == ""
      ) {
        that.$message.warning("名称不能为空！");
        return;
      }
      if (that.AddDeptData.sort == undefined || that.AddDeptData.sort == "") {
        that.$message.warning("排序不能为空！");
        return;
      }
      

      let params = that.AddDeptData;
      let res = await updateWorkMessage(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.dialogFormVisible = !that.dialogFormVisible;
        that.getCompanyToPages(that.seleceDeptToPage.pageIndex);
      }else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async handleClick(id) {
      let that = this;
      that.flag = 2;
      that.disabled = false;
      that.title = "查看核心工作";
      that.dialogFormVisible = !that.dialogFormVisible;
      let params = {
        id: id,
      };
      let res = await getSysWorkById(params);
      if (res.data.Code == 200) {
        that.AddDeptData = res.data.Data;
      }else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async getaddCompany() {
      let that = this;

      if (
        that.AddDeptData.coreName == undefined ||
        that.AddDeptData.coreName == ""
      ) {
        that.$message.warning("名称不能为空！");
        return;
      }
      if (that.AddDeptData.sort == undefined || that.AddDeptData.sort == "") {
        that.$message.warning("排序不能为空！");
        return;
      }

      let params = that.AddDeptData;
      let res = await AddWork(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.dialogFormVisible = false;
        that.getCompanyToPages(that.seleceDeptToPage.pageIndex);
      }else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    handleCurrentChange(val) {
      let that = this;
      that.seleceDeptToPage.pageIndex = val;
      that.getCompanyToPages(val);
    },

    async getCompanyToPages(index) {
      let that = this;
      that.seleceDeptToPage.pageIndex = index;
      let params = that.seleceDeptToPage;
      let res = await getWorkToPage(params);
      if (res.data.Code == 200) {
        that.tableData = res.data.Data;
        that.dataCount = res.data.dataCount;
      }else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },
  },
};
</script>

<style scoped>
.Searchbox {
}
</style>